package com.jams.goodox.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by jams on 2017/6/23.
 * ***
 * Tel: ***
 */

public class TimeUtils {
    public static final String DAY = "DAY";
    public static final String HOUR = "HOUR";
    public static final String MINUTE = "MINUTE";
    public static final String SECOND = "SECOND";

    /**
     * 获取时间差，默认单位是秒
     *
     * @param timeType  时间单位
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return int类型
     */
    public static int getTimeDifference(String timeType, String startTime, String endTime) {
        if (startTime == null || endTime == null) {
            return -1;
        }
        try {
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long from = simpleFormat.parse(startTime).getTime();
            long to = simpleFormat.parse(endTime).getTime();
            int type = 0;
            switch (timeType) {
                case DAY:
                    type = 1000 * 60 * 60 * 24;
                    break;
                case HOUR:
                    type = 1000 * 60 * 60;
                    break;
                case MINUTE:
                    type = 1000 * 60;
                    break;
                case SECOND:
                    type = 1000;
                    break;
                default:
                    type = 1000;
                    break;
            }
            return (int) ((to - from) / type);
        } catch (Exception e) {
            return -1;
        }
    }

    public static String calendar2String(Calendar calendar) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(calendar.getTime());
        } catch (Exception e) {
            return "";
        }
    }

    //获取当前时间
    public static String getSystemTime() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sDateFormat.format(new java.util.Date());
    }

    public static String getTime(int second) {
        if (second < 10) {
            return "00分0" + second + "秒";
        }
        if (second < 60) {
            return /*"00分" + */second + "秒";
        }
        if (second < 3600) {
            int minute = second / 60;
            second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + ":0" + second + "秒";
                }
                return "0" + minute + "分" + second;
            }
            if (second < 10) {
                return minute + "分0" + second + "秒";
            }
            return minute + "分" + second + "秒";
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + "时0" + minute + "分0" + second + "秒";
                }
                return "0" + hour + "时0" + minute + "分" + second + "秒";
            }
            if (second < 10) {
                return "0" + hour + "时" + minute + "分:0" + second + "秒";
            }
            return "0" + hour  + "时" + minute + "分" + second + "秒";
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + "时0" + minute + "分0" + second + "秒";
            }
            return hour + "时0" + minute + "分" + second + "秒";
        }
        if (second < 10) {
            return hour + minute + "分0" + second  + "秒";
        }
        return hour + minute + "分" + second + "秒";
    }


    public static String getDateToString(long milSecond) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }


    /*
     * 毫秒转化
     */
    public static String formatTime(long ms) {

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;

        return strMinute + " 分钟 " + strSecond + " 秒";
    }
}
